package com.dhl.cloud.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dhl.cloud.common.Constants;
import com.dhl.cloud.common.http.HttpResult;
import com.dhl.cloud.common.http.HttpUser;
import com.dhl.cloud.common.http.Page;
import com.dhl.cloud.common.vo.ComFrontBean;
import com.dhl.cloud.course.entity.Notice;
import com.dhl.cloud.course.service.NoticeService;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping(value = "/user")
public class NoticeController extends BaseController{

	@Autowired
	private NoticeService noticeService;
	
    /**
     * 获取我的课程的notice列表
     */
    @GetMapping("getCourseNoticeList")
    public HttpResult getCourseNoticeList(Integer courseid)
    {
        List<Notice> list = noticeService.selectBycourseid(courseid);
        return HttpResult.success("",list);
    }
    @GetMapping("getSysNoticeListLimit")
    public HttpResult getSysNoticeListLimit()
    {
        List<Notice> list = noticeService.selectSysNoticeListLimit();
        return HttpResult.success("",list);
    }
    
    //所有系统notice，分页展示
    @PostMapping("getSysNoticeList")
    public HttpResult getSysNoticeList(HttpServletRequest request,@RequestBody ComFrontBean comFrontBean)
    {
        Integer pageNum = comFrontBean.getPagenum();
		Integer pageSize = comFrontBean.getPagesize();
		PageInfo<Notice> list = noticeService.selectSysNoticeList(pageNum, pageSize);
        Page page = new Page();
		page.setTotal((int)list.getTotal());
		page.setCurrentpage(pageNum);
		page.setPagesize(pageSize);
		return HttpResult.success("",page,list.getList());
    }
    
    @GetMapping("getAllCourseNoticeList")
    public HttpResult getAllCourseNoticeList(Integer courseid)
    {
        List<Notice> list = noticeService.selectAllCourseNoticeList();
        return HttpResult.success("",list);
    }
    @PostMapping("deleteNotice")
    public HttpResult deleteNotice(@RequestBody ComFrontBean ComFrontBean,HttpServletRequest request)
    {
        int flag = noticeService.deleteByPrimaryKey(ComFrontBean.getId());
        return flag==1 ? HttpResult.success("删除成功"):HttpResult.error(Constants.ERROR_MSG_25);
    }
    
    @PostMapping("saveNotice")
    public HttpResult saveNotice(@RequestBody Notice notice,HttpServletRequest request)
    {
    	HttpUser user = getUser(request);
        String username = user.getUsername();
        notice.setUsername(username);
        int flag = noticeService.insert(notice);
        return flag==1 ? HttpResult.success("操作成功"):HttpResult.error(Constants.ERROR_MSG_25);
    }
}
